home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / SECURITY / MNGLR140 / LEXLIB.PAS < prev    next >
Pascal/Delphi Source File  |  1994-07-16  |  13KB  |  452 lines

  1. {Last changes:
  2. 93-03-06  Added unget_str
  3. 93-12-21  Added clear of IOResult to yywrap
  4. 94-01-06  Added check to yywrap for file already closed before closing
  5. }
  6.  
  7. {$IFDEF MSDOS}
  8. {$F+,O+}
  9. {$ENDIF}
  10.  
  11. {$Q-,R-,S-}
  12. unit LexLib;
  13.  
  14. (* Standard Lex library unit for TP Lex Version 3.0.
  15.    2-11-91 AG *)
  16.  
  17. interface
  18.  
  19. (* The Lex library unit supplies a collection of variables and routines
  20.    needed by the lexical analyzer routine yylex and application programs
  21.    using Lex-generated lexical analyzers. It also provides access to the
  22.    input/output streams used by the lexical analyzer and the text of the
  23.    matched string, and provides some utility functions which may be used
  24.    in actions.
  25.  
  26.    This `standard' version of the LexLib unit is used to implement lexical
  27.    analyzers which read from and write to MS-DOS files (using standard input
  28.    and output, by default). It is suitable for many standard applications
  29.    for lexical analyzers, such as text conversion tools or compilers.
  30.  
  31.    However, you may create your own version of the LexLib unit, tailored to
  32.    your target applications. In particular, you may wish to provide another
  33.    set of I/O functions, e.g., if you want to read from or write to memory
  34.    instead to files, or want to use different file types. *)
  35.  
  36. (* Variables:
  37.  
  38.    The variable yytext contains the current match, yyleng its length.
  39.    The variable yyline contains the current input line, and yylineno and
  40.    yycolno denote the current input position (line, column). These values
  41.    are often used in giving error diagnostics (however, they will only be
  42.    meaningful if there is no rescanning across line ends).
  43.  
  44.    The variables yyinput and yyoutput are the text files which are used
  45.    by the lexical analyzer. By default, they are assigned to standard
  46.    input and output, but you may change these assignments to fit your
  47.    target application (use the Turbo Pascal standard routines assign,
  48.    reset, and rewrite for this purpose). *)
  49.  
  50. var
  51.  
  52. yyinput, yyoutput : Text;        (* input and output file *)
  53. yyline            : String;      (* current input line *)
  54. yylineno, yycolno : Integer;     (* current input position *)
  55. yytext            : String;      (* matched text (should be considered r/o) *)
  56. yyleng            : Byte         (* length of matched text *)
  57.   absolute yytext;
  58.  
  59. (* I/O routines:
  60.  
  61.    The following routines get_char, unget_char and put_char are used to
  62.    implement access to the input and output files. Since \n (newline) for
  63.    Lex means line end, the I/O routines have to translate MS-DOS line ends
  64.    (carriage-return/line-feed) into newline characters and vice versa. Input
  65.    is buffered to allow rescanning text (via unput_char).
  66.  
  67.    The input buffer holds the text of the line to be scanned. When the input
  68.    buffer empties, a new line is obtained from the input stream. Characters
  69.    can be returned to the input buffer by calls to unget_char. At end-of-
  70.    file a null character is returned.
  71.  
  72.    The input routines also keep track of the input position and set the
  73.    yyline, yylineno, yycolno variables accordingly.
  74.  
  75.    Since the rest of the Lex library only depends on these three routines
  76.    (there are no direct references to the yyinput and yyoutput files or
  77.    to the input buffer), you can easily replace get_char, unget_char and
  78.    put_char by another suitable set of routines, e.g. if you want to read
  79.    from/write to memory, etc. *)
  80.  
  81. function get_char : Char;
  82.   (* obtain one character from the input file (null character at end-of-
  83.      file) *)
  84.  
  85. procedure unget_char ( c : Char );
  86.   (* return one character to the input file to be reread in subsequent calls
  87.      to get_char *)
  88.  
  89. procedure unget_str(const s : string);
  90.  
  91. procedure put_char ( c : Char );
  92.   (* write one character to the output file *)
  93.  
  94. (* Utility routines: *)
  95.  
  96. procedure echo;
  97.   (* echoes the current match to the output stream *)
  98.  
  99. procedure yymore;
  100.   (* append the next match to the current one *)
  101.  
  102. procedure yyless ( n : Integer );
  103.   (* truncate yytext to size n and return the remaining characters to the
  104.      input stream *)
  105.  
  106. procedure reject;
  107.   (* reject the current match and execute the next one *)
  108.  
  109.   (* reject does not actually cause the input to be rescanned; instead,
  110.      internal state information is used to find the next match. Hence
  111.      you should not try to modify the input stream or the yytext variable
  112.      when rejecting a match. *)
  113.  
  114. procedure return ( n : Integer );
  115. procedure returnc ( c : Char );
  116.   (* sets the return value of yylex *)
  117.  
  118. procedure start ( state : Integer );
  119.   (* puts the lexical analyzer in the given start state; state=0 denotes
  120.      the default start state, other values are user-defined *)
  121.  
  122. (* yywrap:
  123.  
  124.    The yywrap function is called by yylex at end-of-file (unless you have
  125.    specified a rule matching end-of-file). You may redefine this routine
  126.    in your Lex program to do application-dependent processing at end of
  127.    file. In particular, yywrap may arrange for more input and return false
  128.    in which case the yylex routine resumes lexical analysis. *)
  129.  
  130. function yywrap : Boolean;
  131.   (* The default yywrap routine supplied here closes input and output files
  132.      and returns true (causing yylex to terminate). *)
  133.  
  134. (* The following are the internal data structures and routines used by the
  135.    lexical analyzer routine yylex; they should not be used directly. *)
  136.  
  137. var
  138.  
  139. yystate    : Integer; (* current state of lexical analyzer *)
  140. yyactchar  : Char;    (* current character *)
  141. yylastchar : Char;    (* last matched character (#0 if none) *)
  142. yyrule     : Integer; (* matched rule *)
  143. yyreject   : Boolean; (* current match rejected? *)
  144. yydone     : Boolean; (* yylex return value set? *)
  145. yyretval   : Integer; (* yylex return value *)
  146.  
  147. procedure yynew;
  148.   (* starts next match; initializes state information of the lexical
  149.      analyzer *)
  150.  
  151. procedure yyscan;
  152.   (* gets next character from the input stream and updates yytext and
  153.      yyactchar accordingly *)
  154.  
  155. procedure yymark ( n : Integer );
  156.   (* marks position for rule no. n *)
  157.  
  158. procedure yymatch ( n : Integer );
  159.   (* declares a match for rule number n *)
  160.  
  161. function yyfind ( var n : Integer ) : Boolean;
  162.   (* finds the last match and the corresponding marked position and adjusts
  163.      the matched string accordingly; returns:
  164.      - true if a rule has been matched, false otherwise
  165.      - n: the number of the matched rule *)
  166.  
  167. function yydefault : Boolean;
  168.   (* executes the default action (copy character); returns true unless
  169.      at end-of-file *)
  170.  
  171. procedure yyclear;
  172.   (* reinitializes state information after lexical analysis has been
  173.      finished *)
  174.  
  175.  
  176.  
  177. implementation
  178.  
  179. uses {$IFDEF Windows}
  180.      WinDos
  181.      {$ELSE}
  182.      Dos
  183.      {$ENDIF};
  184.  
  185.  
  186. {$IFDEF Windows}
  187. type
  188.   TextRec = TTextRec;
  189. {$ENDIF}
  190.  
  191.  
  192. procedure fatal ( msg : String );
  193.   (* writes a fatal error message and halts program *)
  194.   begin
  195.     writeln('LexLib: ', msg);
  196.     halt(1);
  197.   end(*fatal*);
  198.  
  199. (* I/O routines: *)
  200.  
  201. const nl = #10;  (* newline character *)
  202.  
  203. const max_chars = 2048;
  204.  
  205. var
  206.  
  207. bufptr : Integer;
  208. buf    : array [1..max_chars] of Char;
  209.  
  210. function get_char : Char;
  211.   var i : Integer;
  212.   begin
  213.     if (bufptr=0) and not eof(yyinput) then
  214.       begin
  215.         readln(yyinput, yyline);
  216.         inc(yylineno); yycolno := 1;
  217.         buf[1] := nl;
  218.         for i := 1 to length(yyline) do
  219.           buf[i+1] := yyline[length(yyline)-i+1];
  220.         inc(bufptr, length(yyline)+1);
  221.       end;
  222.     if bufptr>0 then
  223.       begin
  224.         get_char := buf[bufptr];
  225.         dec(bufptr);
  226.         inc(yycolno);
  227.       end
  228.     else
  229.       get_char := #0;
  230.   end(*get_char*);
  231.  
  232. procedure unget_char ( c : Char );
  233.   begin
  234.     if bufptr=max_chars then fatal('input buffer overflow');
  235.     inc(bufptr);
  236.     dec(yycolno);
  237.     buf[bufptr] := c;
  238.   end(*unget_char*);
  239.  
  240. procedure unget_str(const s : string);
  241. var
  242.   i : word;
  243. begin
  244.   for i := length(s) downto 1 do
  245.     unget_char(s[i]);
  246. end;
  247.  
  248.  
  249. procedure put_char ( c : Char );
  250.   begin
  251.     if c=#0 then
  252.       { ignore }
  253.     else if c=nl then
  254.       writeln(yyoutput)
  255.     else
  256.       write(yyoutput, c)
  257.   end(*put_char*);
  258.  
  259. (* Variables:
  260.  
  261.    Some state information is maintained to keep track with calls to yymore,
  262.    yyless, reject, start and yymatch/yymark, and to initialize state
  263.    information used by the lexical analyzer.
  264.    - yystext: contains the initial contents of the yytext variable; this
  265.      will be the empty string, unless yymore is called which sets yystext
  266.      to the current yytext
  267.    - yysstate: start state of lexical analyzer (set to 0 during
  268.      initialization, and modified in calls to the start routine)
  269.    - yylstate: line state information (1 if at beginning of line, 0
  270.      otherwise)
  271.    - yystack: stack containing matched rules; yymatches contains the number of
  272.      matches
  273.    - yypos: for each rule the last marked position (yymark); zeroed when rule
  274.      has already been considered
  275.    - yysleng: copy of the original yyleng used to restore state information
  276.      when reject is used *)
  277.  
  278. const
  279.  
  280. max_matches = 1024;
  281. max_rules   = 256;
  282.  
  283. var
  284.  
  285. yystext            : String;
  286. yysstate, yylstate : Integer;
  287. yymatches          : Integer;
  288. yystack            : array [1..max_matches] of Integer;
  289. yypos              : array [1..max_rules] of Integer;
  290. yysleng            : Byte;
  291.  
  292. (* Utilities: *)
  293.  
  294. procedure echo;
  295.   var i : Integer;
  296.   begin
  297.     for i := 1 to yyleng do
  298.       put_char(yytext[i])
  299.   end(*echo*);
  300.  
  301. procedure yymore;
  302.   begin
  303.     yystext := yytext;
  304.   end(*yymore*);
  305.  
  306. procedure yyless ( n : Integer );
  307.   var i : Integer;
  308.   begin
  309.     for i := yyleng downto n+1 do
  310.       unget_char(yytext[i]);
  311.     yyleng := n;
  312.   end(*yyless*);
  313.  
  314. procedure reject;
  315.   var i : Integer;
  316.   begin
  317.     yyreject := true;
  318.     for i := yyleng+1 to yysleng do
  319.       yytext := yytext+get_char;
  320.     dec(yymatches);
  321.   end(*reject*);
  322.  
  323. procedure return ( n : Integer );
  324.   begin
  325.     yyretval := n;
  326.     yydone := true;
  327.   end(*return*);
  328.  
  329. procedure returnc ( c : Char );
  330.   begin
  331.     yyretval := ord(c);
  332.     yydone := true;
  333.   end(*returnc*);
  334.  
  335. procedure start ( state : Integer );
  336.   begin
  337.     yysstate := state;
  338.   end(*start*);
  339.  
  340. (* yywrap: *)
  341.  
  342. function yywrap : Boolean;
  343. var
  344.   d : word;
  345.   begin
  346.     d := IOResult;
  347.     if TextRec(yyinput).Mode <> fmClosed then  close(yyinput);
  348.     d := IOResult;
  349.     if TextRec(yyoutput).Mode <> fmClosed then  close(yyoutput);
  350.     d := IOResult;
  351.     yywrap := true;
  352.   end(*yywrap*);
  353.  
  354. (* Internal routines: *)
  355.  
  356. procedure yynew;
  357.   begin
  358.     if yylastchar<>#0 then
  359.       if yylastchar=nl then
  360.         yylstate := 1
  361.       else
  362.         yylstate := 0;
  363.     yystate := yysstate+yylstate;
  364.     yytext  := yystext;
  365.     yystext := '';
  366.     yymatches := 0;
  367.     yydone := false;
  368.   end(*yynew*);
  369.  
  370. procedure yyscan;
  371.   begin
  372.     if yyleng=255 then
  373.       fatal('yytext overflow');
  374.     yyactchar := get_char;
  375.     inc(yyleng);
  376.     yytext[yyleng] := yyactchar;
  377.   end(*yyscan*);
  378.  
  379. procedure yymark ( n : Integer );
  380.   begin
  381.     if n>max_rules then fatal('too many rules');
  382.     yypos[n] := yyleng;
  383.   end(*yymark*);
  384.  
  385. procedure yymatch ( n : Integer );
  386.   begin
  387.     inc(yymatches);
  388.     if yymatches>max_matches then fatal('match stack overflow');
  389.     yystack[yymatches] := n;
  390.   end(*yymatch*);
  391.  
  392. function yyfind ( var n : Integer ) : Boolean;
  393.   begin
  394.     yyreject := false;
  395.     while (yymatches>0) and (yypos[yystack[yymatches]]=0) do
  396.       dec(yymatches);
  397.     if yymatches>0 then
  398.       begin
  399.         yysleng := yyleng;
  400.         n       := yystack[yymatches];
  401.         yyless(yypos[n]);
  402.         yypos[n] := 0;
  403.         if yyleng>0 then
  404.           yylastchar := yytext[yyleng]
  405.         else
  406.           yylastchar := #0;
  407.         yyfind := true;
  408.       end
  409.     else
  410.       begin
  411.         yyless(0);
  412.         yylastchar := #0;
  413.         yyfind := false;
  414.       end
  415.   end(*yyfind*);
  416.  
  417. function yydefault : Boolean;
  418.   begin
  419.     yyreject := false;
  420.     yyactchar := get_char;
  421.     if yyactchar<>#0 then
  422.       begin
  423.         put_char(yyactchar);
  424.         yydefault := true;
  425.       end
  426.     else
  427.       begin
  428.         yylstate := 1;
  429.         yydefault := false;
  430.       end;
  431.     yylastchar := yyactchar;
  432.   end(*yydefault*);
  433.  
  434. procedure yyclear;
  435.   begin
  436.     bufptr := 0;
  437.     yysstate := 0;
  438.     yylstate := 1;
  439.     yylastchar := #0;
  440.     yytext := '';
  441.     yystext := '';
  442.   end(*yyclear*);
  443.  
  444.  
  445. begin
  446.   assign(yyinput, '');
  447.   assign(yyoutput, '');
  448.   reset(yyinput); rewrite(yyoutput);
  449.   yylineno := 0;
  450.   yyclear;
  451. end(*LexLib*).
  452.